// console.log(window.location.href);

document.addEventListener("DOMContentLoaded", function () {
  console.log(localStorage.getItem("data"));
  let idz = localStorage.getItem("data");
  axios.defaults.baseURL = "http://cba.itlike.com/public/index.php?s=/api/";
  axios({
    method: "get",
    url: `goods/detail&goodsId=${idz}`,
  }).then(({
    data: {
      data
    }
  }) => {
    console.log(data);

    // 轮播图
    data.detail.goods_images.forEach((ele) => {
      console.log(ele);
      document.querySelector(
        ".swiper-wrapper"
      ).innerHTML += `<div class="swiper-slide">
            <img src="${ele.external_url}" width="100%" alt="" />
        </div>`;
    });
    // swiper轮播图必要代码
    var mySwiper = new Swiper(".swiper", {
      speed: 300,
      loop: true,
      autoplay: 200,
      autoplay: {
        disableOnInteraction: false,
        delay: 800,
      },
      pagination: {
        el: ".swiper-pagination",
      },

    });
    var mySwiper = new Swiper(".swiper", {
      speed: 300,
      loop: true,
      autoplay: 200,
      autoplay: {
        disableOnInteraction: false,
        delay: 800,
      },
      pagination: {
        el: ".swiper-pagination1",
        type: "fraction",
      },
    });

    // 获取商品名称
    let line_price = "";
    if (data.detail.line_price_max !== "0.00") {
      line_price = `
      <del>￥${data.detail.line_price_max}</del>
      `;
    }
    document.querySelector(".classify-goods").innerHTML = `
        <div class="money">
          <p><i>￥</i>${data.detail.goods_price_max}${line_price}</p>
          <p>已售${data.detail.goods_sales}件</p>
        </div>
           <p class="ellipsis-2">${data.detail.goods_name}</p>
    `;

    // 商品介绍图片
    document.querySelector(".photo").innerHTML = `
    <div style="width:100vw!important">
    <p>商品描述</p>
    ${data.detail.content}</div>
    `;
  });

  // axios({
  //   method: "get",
  //   url: "comment/total&goodsId=10040",
  //   params: {
  //     // goodsId:
  //   },
  // }).then(({ data: { data } }) => {
  //   console.log(data.total.all);

  // });

  // 评论
  axios({
    method: "get",
    url: `comment/listRows&goodsId=${idz}&limit=2`,
  }).then(({
    data: {
      data
    }
  }) => {
    console.log(data);
    if (data.total !== 0) {
      document.querySelector(
        ".appraise-top"
      ).innerHTML = ` <p>商品评价(${data.total}条)</p>
    <a href="">查看更多<i class="iconfont icon-youjiantou"></i></a>
`;
      data.list.forEach((item) => {
        console.log(item);
        document.querySelector(".appraise-bottom").innerHTML += `
        <div class='conten'>
        <div>
      <img src="${item.user.avatar_url}" alt="">
      <p>${item.user.nick_name}        </p>

      </div>
      <p>${item.content}</p>
      <p>${item.create_time}</p></div>`;
      });
    } else {
      document.querySelector(".appraise").style.display = "none";
    }
  });
});